﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Common;

namespace Finishing
{
    public partial class MainForm : Form
    {
        public class CmdType
        {
            public const string Clear = "clear", Filter = "filter";
        }

        public string SelectedFileFullPath
        {
            get
            {
                if (treeViewDir.SelectedNode != null)
                {

                    return Utility.FinishPath + treeViewDir.SelectedNode.FullPath;
                }
                return string.Empty;
            }
        }
        /// <summary>
        /// 存在的所有后缀
        /// </summary>
        List<string> ExcList = new List<string>();
        /// <summary>
        /// 存在的后缀及文件地址
        /// </summary>
        Dictionary<string, List<string>> ExistedFileDic = new Dictionary<string, List<string>>();

        public MainForm()
        {
            InitializeComponent();
        }

        private void GetDirTreeView(string path, TreeNode parant)
        {
            string[] dirs = Directory.GetDirectories(path);
            string[] files = Directory.GetFiles(path);
            int i = path.Length + 1; //路径长度

            //判断是否包含子文件夹
            if (dirs.Length != 0)
            {
                foreach (string dir in dirs)
                {
                    TreeNode dirNode = new TreeNode(dir.Substring(i));
                    if (parant != null)
                    {
                        parant.Nodes.Add(dirNode);
                    }
                    else
                    {
                        treeViewDir.Nodes.Add(dirNode);
                    }
                    //如果子文件夹包含文件或文件夹，则递归
                    if (Directory.GetFileSystemEntries(dir).Length != 0)
                    {
                        GetDirTreeView(dir, dirNode);
                    }
                }
            }

            //判断是否包含文件
            if (files.Length != 0)
            {

                foreach (string file in files)
                {
                    FileAdditionalProcessing(file);

                    TreeNode dirNode = new TreeNode(file.Substring(i)) { Tag = file };

                    if (parant != null)
                    {
                        parant.Nodes.Add(dirNode);
                    }
                    else
                    {
                        treeViewDir.Nodes.Add(dirNode);
                    }
                }
            }

        }

        private void FileAdditionalProcessing(string p)
        {
            FileExtensionStatics(p);
            RibbishFileProcessing(p);
        }
        
        /// <summary>
        /// 处理垃圾文件
        /// </summary>
        private void RibbishFileProcessing(string p)
        {
            if (TextFilter.IsRibbishFile(p))
            {
                AppendLog(p);
            }
        }
        /// <summary>
        /// 文件统计
        /// </summary>
        private void FileExtensionStatics(string p)
        {
            string exten = Path.GetExtension(p);
            if (ExistedFileDic.ContainsKey(exten))
            {
                ExistedFileDic[exten].Add(p);
            }
            else
            {
                ExistedFileDic.Add(exten, new List<string> { p });
            }

        }


        private void AppendLog(string par)
        {
            RT_Result.AppendText(par + Environment.NewLine);
            RT_Result.ScrollToCaret();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            Utility.FinishPath = "c:\\1\\";
            GetDirTreeView("c:\\1", null);
            AppendLog(Expand.ToArrayString(ExcList));
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void 分析ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AppendLog(treeViewDir.SelectedNode.Text);
            AppendLog(SelectedFileFullPath);
            FileView.AppendText(Utility.FileString(SelectedFileFullPath));
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Utility.DeleteFile(SelectedFileFullPath);
        }







        /// <summary>
        /// 命令支持
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbCommand_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode== Keys.Enter)
            {
                switch ((sender as TextBox).Text.ToLower())
                {
                    case CmdType.Clear:
                        RT_Result.Clear();
                        (sender as TextBox).SelectAll();
                        break;
                }
            }
        }


    }
}
